﻿var User = require("./../../CClass/class/User");
var schedule = require("node-schedule");
var io = require('socket.io');
var gameConfig = require("./../config/gameConfig")
var gameDao = require("./../../CClass/dao/gameDao");
var log = require("./../../util/loginfo").getInstand;

var Sever = function(){

	//当前比赛剩余时间
	this.isSendEndMsg = false;
	this.matchId = 0;
	this.MatchLogin = true;
	this.isSendPrize = false;
	this.cleanRank = false;
	this.ApplyFlag = true;

	this.init = function(){

		//创建桌子
		//暂定100张
		this.seatMax = gameConfig.seatMax;				//座位最大数量
		this.tableMax = gameConfig.tableMax;
		this.tableList = new Array(this.tableMax);
		this.onlienPepole = 0;
		this.roomid = 1;
		
		//每桌一个数组
		this.state = gameConfig.gameState.noting;
		this.state = new Array(this.tableMax);

		//初始化桌子
		for(var i = 0 ; i < this.tableMax ;i++){
			this.tableList[i] = new Array(this.seatMax + 1);
			this.state[i] = gameConfig.gameState.noting;
		}

		this.cardPoint = [];
		//初始化扑克牌权利值
		for(var i = 0  ; i < 4 ; ++i){
			for (var j = 1 ; j <= 13 ; j++){
				this.cardPoint[i * 13 + j] = ((j - 1) * 4) + 1 + i;
			}
		}

		log.info(gameConfig.gameName + "初始化完毕");
	}


	//是否可以登录
	this.ApplyLogin = function(){
		//如果申请登录玩家当场有下注并在发奖期
		if (this.state == gameConfig.gameState.open){
			return false;
		}else{
			return true;
		}
	}

	//设置io
	this.setIo = function(_io,_Csocket){
		this.io = _io;
		this.Csocket = _Csocket;
	}


	//进入房间
	this.LoginRoom = function(_user,_socket,userlist){
		if (this.onlienPepole > this.tableMax * this.seatMax){
			 log.warn("已经大于当前房间人数");
			 return {tableId:-1,seatId:-1};
		}
		++this.onlienPepole;
		var tableidx = -1;
		var seatidx = -1;
		var nullseat = false;
		if (_user._Robot) {
			for (var i = 0; i < this.tableMax; i++) {
				var TablePlayers = this.getTablePlayers(i);
				var robot_num = 0;
				var ren_num = 0;
				for (var rn in TablePlayers) {
					if (userlist) {
						if(userlist[TablePlayers[rn]]){
							if (userlist[TablePlayers[rn]]._Robot) {
								robot_num++
							}else {
								ren_num++
							}
						}
					}
				}
				var random_n=RandomNumBoth(1,3);
				if (robot_num < random_n) {
					for (var j = 0; j < this.seatMax; j++) {
						if (!this.tableList[i][j]) {
							nullseat = true;
							tableidx = i;
							seatidx = j;
							break;
						}
					}
					if (nullseat) {
						break;
					}
				}
			}
		}else {
			for(var i = 0; i < this.tableMax ; i++){
				for (var j = 0 ; j < this.seatMax; j++){
					if (!this.tableList[i][j]){
						nullseat = true;
						tableidx = i;
						seatidx = j;
						break;
					}
				}
				if (nullseat){
					break;
				}
			}
		}


		if (tableidx == -1 || seatidx == -1){
			log.err("error:找座位出错");
			return {tableId:-1,seatId:-1};
		}

		//log.info("座位:" + tableidx + "桌," + seatidx + "个位置");
		var tablestring = "table"+tableidx;
		_user.loginTable(tableidx);
		_user.loginSeat(seatidx);

		//分配座位
		//寻找空座位
		this.tableList[tableidx][seatidx] = _socket.userId;
		this.tableList[tableidx][this.seatMax] = 1; 

		//把当前socket加入当前桌子分组
		var tablestring = "table"+tableidx;
		_socket.join(tablestring);	

		return {tableId:tableidx,seatId:seatidx};
	}

	//离开房间
	this.LogoutRoom = function(_User,_socket){
		//log.info("有人离开桌子");
		var tableid = _User.getTable();
		var seatid = _User.getSeat()
		//没有任何座位信息
		if (tableid == -1 || seatid == -1) return;
		//log.info("座位," + tableid + "桌," + seatid + "个位置");
		//分配座位
		//寻找空座位
		this.tableList[tableid][seatid] = null;
		--this.onlienPepole;

		//检测当前桌子还是否有人
		var isOnline = false;
		for(var i = 0 ; i < this.seatMax ; i++){
			if (this.tableList[tableid][i]){
				isOnline = true;
				break;
			}
		}
		if (!isOnline){
			this.tableList[tableid][this.seatMax] = 0;
		}

		var tablestring = "table"+tableid;
		_socket.leave(tablestring);	
	}

	//断线
	this.lineOut = function(_User,_socket){
		var tableid = _User.getTable();
		var tablestring = "table"+tableid;
		_socket.leave(tablestring);	
	}

	//进入房间
	this.LoginRoombyLineOut = function(_user,_socket,_tableidx,_seatidx){
		var tableidx = _tableidx;
		var seatidx = _seatidx;

		var tablestring = "table"+tableidx;
		_user.loginTable(tableidx);
		_user.loginSeat(seatidx);

		this.tableList[tableidx][seatidx] = _socket.userId;

		//把当前socket加入当前桌子分组
		var tablestring = "table"+tableidx;
		_socket.join(tablestring);	

		return {tableId:tableidx,seatId:seatidx};
	}

	//清理已经断线的用户
	this.cleanLineOut = function(_tableid,_seatid){
		var tableid = _tableid;
		var seatid = _seatid;
		//没有任何座位信息
		if (tableid == -1 || seatid == -1) return;

		this.tableList[tableid][seatid] = null;
		--this.onlienPepole;

		//检测当前桌子还是否有人
		var isOnline = false;
		for(var i = 0 ; i < this.seatMax ; i++){
			if (this.tableList[tableid][i]){
				isOnline = true;
				break;
			}
		}
		if (!isOnline){
			this.tableList[tableid][this.seatMax] = 0;
		}
	}

	this.getTablePlayers = function(_tableId){
		var playerList = [];
		for(var i = 0 ; i < this.seatMax ; i++){
			if (this.tableList[_tableId][i])
				playerList.push(this.tableList[_tableId][i])
		}
		return playerList;
	}

	this.getTablePlayerBySeatId = function(_tableId,_seatId){		
		return this.tableList[_tableId][_seatId];
		
	}

	this.reset = function(tableId){
		//log.info("reset");
		this.state[tableId] = gameConfig.gameState.gameBegin;
	}

	//停止计时
	this.stop = function(tableId){
		this.state[tableId] = gameConfig.gameState.noting;
	}

	this.init();

}


module.exports = Sever;

function RandomNumBoth(Min, Max) {
	//生成指定范围内随机整数
	var Range = Max - Min;
	var Rand = Math.random();
	var num = Min + Math.round(Rand * Range); //四舍五入
	return num;
}